package com.psm.ui;

import java.awt.Color;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.psm.listeners.BrowsePDFActionListener;
import com.psm.listeners.BrowseSrcDirActionListener;
import com.psm.listeners.SplitMergeOptionsListener;
import com.psm.listeners.SplitOptionListener;
import com.psm.listeners.SplitPDFActionListener;

public class MainWindow extends JFrame
{
    private static final long serialVersionUID = 1L;

    private JRadioButton radioSplit;
    private JPanel mergePanel;
    private JTextField txtSelectPDF;
    private JButton btnMergePDF;
    private JButton btnDirBrowse;
    private JTextField txtSrcDirectory;
    private JLabel lblSrcDirectory;
    private JButton btnSplitPDF;
    private JTextField txtToPage;
    private JLabel lblToPage;
    private JTextField txtFromPage;
    private JLabel lblFromPage;
    private JComboBox comboSplitOptions;
    private JLabel lblSplitOptions;
    private JButton btnSrcBrowse;
    private JLabel lblSelectPDF;
    private JRadioButton radioMerge;
    private JPanel splitPanel;
    private TitledBorder splitTitle;
    private TitledBorder mergeTitle;

    {
	// Set Look & Feel
	try
	{
	    javax.swing.UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
	}
	catch ( Exception e )
	{
	    e.printStackTrace();
	}
    }

    private void initGUI()
    {
	try
	{
	    {
		getContentPane().setLayout( null );
		{
		    radioSplit = new JRadioButton();
		    getContentPane().add( radioSplit );
		    radioSplit.setText( "Split PDF" );
		    radioSplit.setSelected( true );
		    radioSplit.setBounds( 10, 11, 67, 23 );
		}
		{
		    splitTitle = new TitledBorder( "Split PDF files" );
		    splitPanel = new JPanel();
		    splitPanel.setBorder( splitTitle );
		    getContentPane().add( splitPanel );
		    splitPanel.setBounds( 10, 41, 525, 113 );
		    splitPanel.setLayout( null );
		    {
			lblSelectPDF = new JLabel();
			splitPanel.add( lblSelectPDF );
			lblSelectPDF.setText( "Please select a PDF file" );
			lblSelectPDF.setBounds( 16, 20, 121, 18 );
		    }
		    {
			txtSelectPDF = new JTextField();
			splitPanel.add( txtSelectPDF );
			txtSelectPDF.setBounds(141, 19, 300, 20);
		    }
		    {
			btnSrcBrowse = new JButton();
			splitPanel.add( btnSrcBrowse );
			btnSrcBrowse.setText( "Browse" );
			btnSrcBrowse.setBounds( 447, 18, 67, 23 );
		    }
		    {
			lblSplitOptions = new JLabel();
			splitPanel.add( lblSplitOptions );
			lblSplitOptions.setText( "Spliting Options" );
			lblSplitOptions.setBounds( 16, 54, 74, 14 );
		    }
		    {
			ComboBoxModel comboSplitOptionsModel = new DefaultComboBoxModel( new String[]
			{ "Split Page by page", "Split Odds and Evens pages", "Extract page" } );
			comboSplitOptions = new JComboBox();
			splitPanel.add( comboSplitOptions );
			comboSplitOptions.setModel( comboSplitOptionsModel );
			comboSplitOptions.setBounds( 16, 74, 178, 20 );
		    }
		    {
			lblFromPage = new JLabel();
			splitPanel.add( lblFromPage );
			lblFromPage.setText( "From" );
			lblFromPage.setBounds( 221, 77, 24, 14 );
			lblFromPage.setEnabled( false );
		    }
		    {
			txtFromPage = new JTextField();
			splitPanel.add( txtFromPage );
			txtFromPage.setBounds( 249, 74, 40, 20 );
			txtFromPage.setEnabled( false );
		    }
		    {
			lblToPage = new JLabel();
			splitPanel.add( lblToPage );
			lblToPage.setText( "To" );
			lblToPage.setBounds( 305, 77, 17, 14 );
			lblToPage.setEnabled( false );
		    }
		    {
			txtToPage = new JTextField();
			splitPanel.add( txtToPage );
			txtToPage.setBounds( 326, 74, 40, 20 );
			txtToPage.setEnabled( false );
		    }
		    {
			btnSplitPDF = new JButton();
			splitPanel.add( btnSplitPDF );
			btnSplitPDF.setText( "Split PDF" );
			btnSplitPDF.setBounds( 404, 73, 105, 23 );
		    }
		}
		{
		    radioMerge = new JRadioButton();
		    getContentPane().add( radioMerge );
		    radioMerge.setText( "Merge PDF" );
		    radioMerge.setBounds( 10, 156, 77, 23 );
		}
		{
		    mergeTitle = new TitledBorder( "Merge PDF files" );
		    mergeTitle.setTitleColor( Color.gray );
		    mergePanel = new JPanel();
		    mergePanel.setBorder( mergeTitle );
		    getContentPane().add( mergePanel );
		    mergePanel.setBounds( 10, 186, 525, 91 );
		    mergePanel.setLayout( null );
		    {
			lblSrcDirectory = new JLabel();
			mergePanel.add( lblSrcDirectory );
			lblSrcDirectory.setText( "Please select a source directory" );
			lblSrcDirectory.setBounds( 16, 20, 152, 14 );
			lblSrcDirectory.setEnabled( false );
		    }
		    {
			txtSrcDirectory = new JTextField();
			mergePanel.add( txtSrcDirectory );
			txtSrcDirectory.setBounds(178, 17, 262, 20);
			txtSrcDirectory.setEnabled( false );
		    }
		    {
			btnDirBrowse = new JButton();
			mergePanel.add( btnDirBrowse );
			btnDirBrowse.setText( "Browse" );
			btnDirBrowse.setBounds( 446, 16, 67, 23 );
			btnDirBrowse.setEnabled( false );
		    }
		    {
			btnMergePDF = new JButton();
			mergePanel.add( btnMergePDF );
			btnMergePDF.setText( "Merge PDFs" );
			btnMergePDF.setBounds( 234, 50, 89, 23 );
			btnMergePDF.setEnabled( false );
		    }
		}
		{
		    radioSplit.addActionListener( new SplitMergeOptionsListener( txtSelectPDF, btnMergePDF, btnDirBrowse, txtSrcDirectory, lblSrcDirectory, btnSplitPDF, txtToPage, lblToPage, txtFromPage, lblFromPage,
			    comboSplitOptions, lblSplitOptions, btnSrcBrowse, lblSelectPDF, radioMerge, radioSplit, splitTitle, mergeTitle, this ) );
		    radioMerge.addActionListener( new SplitMergeOptionsListener( txtSelectPDF, btnMergePDF, btnDirBrowse, txtSrcDirectory, lblSrcDirectory, btnSplitPDF, txtToPage, lblToPage, txtFromPage, lblFromPage,
			    comboSplitOptions, lblSplitOptions, btnSrcBrowse, lblSelectPDF, radioMerge, radioSplit, splitTitle, mergeTitle, this ) );
		    comboSplitOptions.addActionListener( new SplitOptionListener( comboSplitOptions, lblFromPage, txtFromPage, lblToPage, txtToPage ) );
		    btnSrcBrowse.addActionListener( new BrowsePDFActionListener( this, txtSelectPDF ) );
		    btnDirBrowse.addActionListener( new BrowseSrcDirActionListener( this, txtSrcDirectory ) );
		    ButtonGroup buttonGroup = new ButtonGroup();
		    buttonGroup.add( radioSplit );
		    buttonGroup.add( radioMerge );
		    btnSplitPDF.addActionListener( new SplitPDFActionListener( txtSelectPDF, comboSplitOptions, txtFromPage, txtToPage, btnSplitPDF ) );
		}
	    }
	    {
		this.setTitle( "PDF Split Merge v1.0" );
		this.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
		this.setSize( 561, 326 );
		this.setLocationRelativeTo( null );
		this.setVisible( true );
	    }
	}
	catch ( Exception e )
	{
	    e.printStackTrace();
	}
    }

    public MainWindow()
    {
	initGUI();
    }

}
